<template>
  <el-card>
    <template #header><h2>护理级别管理</h2></template>

    <!-- 过滤与操作栏 -->
    <el-form :inline="true" :model="filters" class="toolbar">
      <el-form-item label="状态">
        <el-select v-model="filters.status" placeholder="全部">
          <el-option :label="'启用'" :value="1" />
          <el-option :label="'停用'" :value="0" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="fetchLevels">查询</el-button>
        <el-button @click="openAddDialog">添加</el-button>
      </el-form-item>
    </el-form>

    <!-- 列表 -->
    <el-table :data="levels" stripe>
      <el-table-column prop="levelName" label="名称" />
      <el-table-column prop="status" label="状态">
        <template #default="{ row }">
          <el-tag :type="row.status === 1 ? 'success' : 'info'">
            {{ row.status === 1 ? '启用' : '停用' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template #default="{ row }">
          <el-button size="small" @click="toggleStatus(row)">
            {{ row.status === 1 ? '停用' : '启用' }}
          </el-button>
          <el-button size="small" type="warning" @click="goConfig(row)">配置项目</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      v-model:current-page="page.current"
      :page-size="page.size"
      :total="page.total"
      @current-change="fetchLevels"
    />

    <!-- 添加对话框 -->
    <el-dialog title="添加护理级别" v-model="addDialogVisible">
      <el-form :model="newLevel">
        <el-form-item label="名称" :label-width="'80px'">
          <el-input v-model="newLevel.levelName" />
        </el-form-item>
        <el-form-item label="状态">
          <el-switch v-model="newLevel.status" active-value="1" inactive-value="0" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="addDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="createLevel">确认</el-button>
      </template>
    </el-dialog>
  </el-card>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import axios from 'axios'
import qs from 'qs'

const router = useRouter()
const filters = reactive({ status: null })
const levels = ref([])
const page = reactive({ current: 1, size: 10, total: 0 })
const addDialogVisible = ref(false)
const newLevel = reactive({ levelName: '', status: 1 })

async function fetchLevels() {
  const { data } = await axios.get('/CareLevelController/queryByStatus', {
    params: { status: filters.status, cur: page.current, number: page.size }
  })
  levels.value = data.data
  page.total = data.total
}

function openAddDialog() { addDialogVisible.value = true }

async function createLevel() {
  await axios.post('/CareLevelController/addCareLevel', qs.stringify(newLevel))
  addDialogVisible.value = false
  fetchLevels()
}

async function toggleStatus(row) {
  const newStatus = row.status === 1 ? 0 : 1
  await axios.post('/CareLevelController/updateCareLevel', qs.stringify({ id: row.id, status: newStatus }))
  fetchLevels()
}

function goConfig(row) {
  router.push({ path: '/LevelProjectList', query: { levelId: row.id } })
}

onMounted(fetchLevels)
</script>

<style scoped>
.toolbar { margin-bottom: 16px; }
</style>